package com.czk.springboot.reactive.reactor.sink;

import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

public class EventDemo {

    public static void main(String[] args) throws InterruptedException {
        EventDemo event = new EventDemo();
        event.create();
    }

    public  void create() throws InterruptedException {
        Flux.create(sink -> {
                    MyListener listener = new MyListener(sink);

                    for (int i = 0; i < 10; i++) {
                        int finalI = i;
                        new Thread(() -> {
                            listener.online("用户-" + finalI);
                        }).start();
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                })
                .log()
                .subscribe()
        ;

    }

    static class MyListener {
        FluxSink<Object> sink;

        public MyListener(FluxSink<Object> sink) {
            this.sink = sink;
        }

        public void online(String userName) {
            System.out.println("用户登录了===" + userName);
            sink.next(userName);
        }

    }

}
